<?php

namespace app\oa\controller;

use app\base\BaseController;
use app\load\loadServices\OaServices;
use app\oa\validate\FlowValidate;
use think\Exception;
use think\facade\Db;

/**
 * 审批流程
 */
class Flow extends BaseController
{

    /**
     * 获取审核步骤人员
     * @return mixed
     */
    public function get_flow_users()
    {
        $input = input();
        try {
            $result = OaServices::Flow()->get_flow_users($input['id'] ?? 0);
            return to_assign(0, '', $result);
        } catch (Exception $e) {
            return to_assign(1, $e->getMessage());
        }
    }

    /**
     * 获取审核流程节点
     * @return mixed
     */
    public function get_flow_nodes()
    {
        $input = input();
        try {
            $result = OaServices::FlowRecord()->get_flow_nodes($input['id'] ?? 0, $input['type'] ?? 1);
            return to_assign(0, '', $result);
        } catch (Exception $e) {
            return to_assign(1, $e->getMessage());
        }
    }


    /**
     * 获取审核流程节点
     * @return mixed
     */
    public function get_flow_record()
    {
        $input = input();
        try {
            $result = OaServices::FlowRecord()->list($input['id'] ?? 0, $input['type'] ?? 1);
            return to_assign(0, '', $result);
        } catch (Exception $e) {
            return to_assign(1, $e->getMessage());
        }
    }

    /**
     * 审核
     * [    id: 32
            content: 123
            check_node: 1
            check_admin_ids: 0
            check: 1  |状态  成功  拒绝   撤回
            type: 1  | 类型  日常审核  一个报销审批  一个发票审批  一个合同审批(暂未开发)
            file: storage/base64_image/64d59caa2c05f.png
     * ]
     * @return void
     */
    public function flow_check()
    {
        $input = input();
        try {
            //参数必传入项
            validate(FlowValidate::class)->scene('check')->check($input);
            OaServices::Flow()->save($input, $this->uid);
            return to_assign();
        } catch (Exception $e) {
            return to_assign(1, $e->getMessage());
        }
    }

    //获取关键字
    public function get_keyword_cate()
    {
        $keyword = Db::name('Keywords')->where(['status' => 1])->order('id desc')->select()->toArray();
        return to_assign(0, '', $keyword);
    }


    //
//    //流程审核
//    public function flow_check1()
//    {
//        $param = get_params();
//        $id = $param['id'];
//        $type = $param['type'];
//        $detail = [];
//        $subject = '一个审批';
//
//        if ($type == 1) {
//            //日常审核
//            $detail = Db::name('Approve')->where(['id' => $id])->find();
//            $subject = '一个日常审批';
//            $msg_title_type = $detail['type'];
//        } else if ($type == 2) {
//            //报销审核
//            $detail = Db::name('Expense')->where(['id' => $id])->find();
//            $subject = '一个报销审批';
//            $msg_title_type = 22;
//        } else if ($type == 3) {
//            //发票审核
//            $detail = Db::name('Invoice')->where(['id' => $id])->find();
//            $subject = '一个发票审批';
//            $msg_title_type = 23;
//        } else if ($type == 4) {
//            //合同审核
//            $detail = Db::name('Contract')->where(['id' => $id])->find();
//            $subject = '一个合同审批';
//            $msg_title_type = 24;
//        }
//        if (empty($detail)) {
//            return to_assign(1, '审批数据错误');
//        }
//        //当前审核节点详情
//        $step = Db::name('FlowStep')->where(['action_id'=>$id,'type'=>$type,'sort'=>$detail['check_step_sort'],'delete_time'=>0])->find();
//
//        //审核通过
//        if($param['check'] == 1){
//            $check_admin_ids = explode(",", strval($detail['check_admin_ids']));
//            if (!in_array($this->uid, $check_admin_ids)) {
//                return to_assign(1, '您没权限审核该审批');
//            }
//
//            //多人会签审批
//            if ($step['flow_type'] == 4) {
//                //查询当前会签记录数
//                $check_count = Db::name('FlowRecord')->where(['action_id'=>$id,'type'=>$type,'step_id'=>$step['id']])->count();
//                //当前会签记应有记录数
//                $flow_count = explode(',', $step['flow_uids']);
//                if (($check_count + 1) >= count($flow_count)) {
//                    $next_step = Db::name('FlowStep')->where(['action_id' => $id, 'type' => $type, 'sort' => ($detail['check_step_sort'] + 1), 'delete_time' => 0])->find();
//                    if ($next_step) {
//                        //存在下一步审核
//                        $param['check_admin_ids'] = $next_step['flow_uids'];
//                        $param['check_step_sort'] = $detail['check_step_sort'] + 1;
//                        $param['check_status'] = 1;
//                    } else {
//                        //不存在下一步审核，审核结束
//                        $param['check_status'] = 2;
//                        $param['check_admin_ids'] = '';
//                    }
//                } else {
//                    $param['check_status'] = 1;
//                    $param['check_admin_ids'] = $step['flow_uids'];
//                }
//            } else if ($step['flow_type'] == 0) {
//                //自由人审批
//                if ($param['check_node'] == 2) {
//                    $next_step = $detail['check_step_sort'] + 1;
//                    $flow_step = array(
//                        'action_id' => $id,
//                        'sort' => $next_step,
//                        'type' => $type,
//                        'flow_uids' => $param['check_admin_ids'],
//                        'create_time' => time()
//                    );
//                    $fid = Db::name('FlowStep')->strict(false)->field(true)->insertGetId($flow_step);
//                    //下一步审核步骤
//                    $param['check_admin_ids'] = $param['check_admin_ids'];
//                    $param['check_step_sort'] = $next_step;
//                    $param['check_status'] = 1;
//                } else {
//                    //不存在下一步审核，审核结束
//                    $param['check_status'] = 2;
//                    $param['check_admin_ids'] = '';
//                }
//            } else {
//                $next_step = Db::name('FlowStep')->where(['action_id' => $id, 'type' => $type, 'sort' => ($detail['check_step_sort'] + 1), 'delete_time' => 0])->find();
//                if ($next_step) {
//                    //存在下一步审核
//                    if ($next_step['flow_type'] == 1) {
//                        $param['check_admin_ids'] = get_department_leader($detail['admin_id']);
//                    } else if ($next_step['flow_type'] == 2) {
//                        $param['check_admin_ids'] = get_department_leader($detail['admin_id'], 1);
//                    } else {
//                        $param['check_admin_ids'] = $next_step['flow_uids'];
//                    }
//                    $param['check_step_sort'] = $detail['check_step_sort'] + 1;
//                    $param['check_status'] = 1;
//                } else {
//                    //不存在下一步审核，审核结束
//                    $param['check_status'] = 2;
//                    $param['check_admin_ids'] = '';
//                }
//            }
//            if ($param['check_status'] == 1 && empty($param['check_admin_ids'])) {
//                return to_assign(1, '找不到下一步的审批人，该审批流程设置有问题，请联系HR或者管理员');
//            }
//            //审核通过数据操作
//            $param['last_admin_id'] = $this->uid;
//            $param['flow_admin_ids'] = $detail['flow_admin_ids'] . $this->uid . ',';
//
//            if ($type == 1) {
//                //日常审核
//                $res = Db::name('Approve')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
//            } else if ($type == 2) {
//                //报销审核
//                $res = Db::name('Expense')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
//            } else if ($type == 3) {
//                //发票审核
//                $res = Db::name('Invoice')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
//            } else if ($type == 4) {
//                //合同审核
//                $res = Db::name('Contract')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
//            }
//
//            if ($res !== false) {
//                $checkData = array(
//                    'action_id' => $id,
//                    'step_id' => $step['id'],
//                    'check_user_id' => $this->uid,
//                    'type' => $type,
//                    'check_time' => time(),
//                    'status' => $param['check'],
//                    'content' => $param['content'],
//                    'create_time' => time()
//                );
//
//                $checkData['image'] = $param['file'] ?? '';
//
//                $aid = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
//                add_log('check', $param['id'], $param, $subject);
//                //发送消息通知
//                $msg = [
//                    'create_time' => date('Y-m-d H:i:s', $detail['create_time']),
//                    'action_id' => $id,
//                    'title' => Db::name('FlowType')->where('id', $msg_title_type)->value('title'),
//                    'from_uid' => $detail['admin_id']
//                ];
//                if ($param['check_status'] == 1) {
//                    $users = $param['check_admin_ids'];
//                    sendMessage($users, ($type * 10 + 11), $msg);
//                }
//                if ($param['check_status'] == 2) {
//                    $users = $detail['admin_id'];
//                    sendMessage($users, ($type * 10 + 12), $msg);
//                }
//                return to_assign();
//            } else {
//                return to_assign(1, '操作失败');
//            }
//        }
//        else if ($param['check'] == 2) {
//            $check_admin_ids = explode(",", strval($detail['check_admin_ids']));
//            if (!in_array($this->uid, $check_admin_ids)) {
//                return to_assign(1, '您没权限审核该审批');
//            }
//            //拒绝审核，数据操作
//            $param['check_status'] = 3;
//            $param['last_admin_id'] = $this->uid;
//            $param['flow_admin_ids'] = $detail['flow_admin_ids'] . $this->uid . ',';
//            $param['check_admin_ids'] = '';
//            if ($step['flow_type'] == 5) {
//                //获取上一步的审核信息
//                $prev_step = Db::name('FlowStep')->where(['action_id' => $id, 'type' => $type, 'sort' => ($detail['check_step_sort'] - 1), 'delete_time' => 0])->find();
//                if ($prev_step) {
//                    //存在上一步审核
//                    $param['check_step_sort'] = $prev_step['sort'];
//                    $param['check_admin_ids'] = $prev_step['flow_uids'];
//                    $param['check_status'] = 1;
//                } else {
//                    //不存在上一步审核，审核初始化步骤
//                    $param['check_step_sort'] = 0;
//                    $param['check_admin_ids'] = '';
//                    $param['check_status'] = 0;
//                }
//            }
//            if ($type == 1) {
//                //日常审核
//                $res = Db::name('Approve')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
//            } else if ($type == 2) {
//                //报销审核
//                $res = Db::name('Expense')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
//            } else if ($type == 3) {
//                //发票审核
//                $res = Db::name('Invoice')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
//            } else if ($type == 4) {
//                //合同审核
//                $res = Db::name('Contract')->strict(false)->field('check_step_sort,check_status,last_admin_id,flow_admin_ids,check_admin_ids')->update($param);
//            }
//            if ($res !== false) {
//                $checkData = array(
//                    'action_id' => $id,
//                    'step_id' => $step['id'],
//                    'check_user_id' => $this->uid,
//                    'type' => $type,
//                    'check_time' => time(),
//                    'status' => $param['check'],
//                    'content' => $param['content'],
//                    'create_time' => time()
//                );
//
//                $checkData['image'] = $param['file'] ?? '';
//
//                $aid = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
//                add_log('refue', $param['id'], $param, $subject);
//                //发送消息通知
//                $msg = [
//                    'create_time' => date('Y-m-d H:i:s', $detail['create_time']),
//                    'action_id' => $detail['id'],
//                    'title' => Db::name('FlowType')->where('id', $msg_title_type)->value('title'),
//                    'from_uid' => $detail['admin_id']
//                ];
//                $users = $detail['admin_id'];
//                sendMessage($users, ($type * 10 + 13), $msg);
//                return to_assign();
//            } else {
//                return to_assign(1, '操作失败');
//            }
//        } //撤销审核，数据操作
//        else if ($param['check'] == 3) {
//            if ($detail['admin_id'] != $this->uid) {
//                return to_assign(1, '你没权限操作');
//            }
//            //撤销审核，数据操作
//            $param['check_status'] = 4;
//            $param['check_admin_ids'] = '';
//            $param['check_step_sort'] = 0;
//
//            if ($type == 1) {
//                //日常审核
//                $res = Db::name('Approve')->strict(false)->field('check_step_sort,check_status,check_admin_ids')->update($param);
//            } else if ($type == 2) {
//                //报销审核
//                $res = Db::name('Expense')->strict(false)->field('check_step_sort,check_status,check_admin_ids')->update($param);
//            } else if ($type == 3) {
//                //发票审核
//                $res = Db::name('Invoice')->strict(false)->field('check_step_sort,check_status,check_admin_ids')->update($param);
//            } else if ($type == 4) {
//                //合同审核
//                $res = Db::name('Contract')->strict(false)->field('check_step_sort,check_status,check_admin_ids')->update($param);
//            }
//            if ($res !== false) {
//                $checkData = array(
//                    'action_id' => $id,
//                    'step_id' => 0,
//                    'check_user_id' => $this->uid,
//                    'type' => $type,
//                    'check_time' => time(),
//                    'status' => $param['check'],
//                    'content' => $param['content'],
//                    'create_time' => time()
//                );
//                if ($type == 1) {
//                    $checkData['image'] = $param['file'] ?? '';
//                }
//                $aid = Db::name('FlowRecord')->strict(false)->field(true)->insertGetId($checkData);
//                add_log('back', $param['id'], $param, $subject);
//                return to_assign();
//            } else {
//
//                return to_assign(1, '操作失败');
//            }
//        }
//    }

}